﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Autenticazione.BusinessObject;

namespace Autenticazione
{
    public partial class ListaIscritti : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Fights fights = null;

            if (!IsPostBack)
            {
                fights = new Fights();
                Tools.Tools.FillFights(cboFight);
                LoadGrid("-1");
                //Response.Write(string.Format("OK --> Not PostBack; SelectedValue={0}\n\r", "0"));
                //Response.Write(string.Format("CurrentPage={0} PageSize={1}", dtgFights.CurrentPageIndex.ToString(), dtgFights.PageSize.ToString()));
            }
            
        }

        protected void cboFight_Changed(object sender, EventArgs e)
        {
            LoadGrid(cboFight.SelectedValue);
            //Response.Write(string.Format("OK --> PostBack; SelectedValue={0}\n\r", cboFight.SelectedValue));
            //Response.Write(string.Format("CurrentPage={0} PageSize={1}", dtgFights.CurrentPageIndex.ToString(), dtgFights.PageSize.ToString()));
        }

        private void LoadGrid(string selected)
        {
            BusinessObject.Fights f = new BusinessObject.Fights();
            DataTable dt = null;
            DataTable dtView = null;

            dt = f.GetAllTbFights(selected);

            dtView = dt.Clone();

            for (int i = dtgFights.CurrentPageIndex * dtgFights.PageSize; i <= (dtgFights.CurrentPageIndex + 1) * dtgFights.PageSize - 1; i++)
            {
                if (i <= dt.Rows.Count - 1)
                    dtView.ImportRow(dt.Rows[i]);
            }

            dtgFights.VirtualItemCount = dt.Rows.Count;

            dtgFights.DataSource = dtView;
            dtgFights.DataBind();
        }

        protected string GetIscrizione(int i, object iscrizione)
        {
            return iscrizione.ToString().Substring(i - 1, 1) == "1" ? @"<img src='Image/Selected.Png' />" : @"<img src='Image/NotSelected.Png' />";
        }

        protected bool GetSelected(int i, object iscrizione)
        {
            return iscrizione.ToString().Substring(i - 1, 1) == "1" ? true : false;
        }

        protected void dtgFights_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dtgFights.CurrentPageIndex = e.NewPageIndex;
            dtgFights.EditItemIndex = -1;
            LoadGrid(cboFight.SelectedValue);
        }
    }
}